<html>
<script>
    // var data = [1, 1, 3, 5, 5];  // 这里是指待处理的数组
    // var total = 0;
    // // 平均数是所有元素的累加和值除以元素个数

    // for (var i = 0; i < data.length; i++) {
    //     total += data[i];

    // }
    // var meam = total / data.length
    // console.log(meam);

    // 上例简单的例子可以用map()和reduce()来实现同样的计算
    // 首先定义两个简单的函数
    var sum = function (x, y) { return x + y  };
    var square = function (x, y) { return x * y };



    function mapCopy(a, f) {
        console.log(a.length);
        var returns = []
        for (var i = 0;  i < a.length; i++) {
            console.log(i in a);
            if (i in a) {
                returns[i] = f.call(null, a[i], i, a);
            }
        }
        console.log(returns);
        return returns;

    }

    // data.push({ "mapCopy": mapCopy })

    // 然后将这些函数和数组方法配合计算出平均数和标准差
    var data = [7, 2, 3, 5, 5];
    var meam = reduceCopy(data,sum) / data.length;
    // console.log(data.mapCopy)
    var deviations = mapCopy(data, function (x) { return x - meam });
    // console.log(deviations);
    var stddev = Math.sqrt(reduceCopy(mapCopy(deviations,square),sum) / data.length - 1);


    function reduceCopy(a, f, iniital) {
        console.log(arguments);
        var i = 0, len = a.length, accumulator = 1;
        console.log(a.length,i,len);
        // 以特定的初始值开始，否则第一个值取自a
        if (arguments.length > 2) {
            accumulator = initial;
        } else {
            if (len == 0) { throw TypeError() };
            console.log("success")
            while (i < len) {
                if (i in a) {
                    accumulator = a[i++];
                    break;
                } else { i++; };
            }
            if (i == len) { throw TypeError() };
        }
        console.log(accumulator);
        // 对于数组中剩余的元素依次调用
        while (i < len) {
            console.log("循环次数",i)
            if (i in a) {
                accumulator = f.call(undefined, accumulator, a[i], i, a);
            }
            i++;
           
        }
        console.log(accumulator);
        return accumulator;
    }


</script>


<head>
    <title>放置文章标题</title>
</head>

<body>

</body>

</html>